package labelsystem.Service.Implements;/**
 * @projectName careOlder
 * @Description TODO
 * @author MCG
 * @time 2023/3/29 15:00
 */

import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import labelsystem.Mapper.*;
import labelsystem.Pojo.*;
import labelsystem.Service.ouserService;
import labelsystem.utils.monitorHandle;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;

/**
 *description: TODO
 *@author MCG
 *@date 2023/3/29 15:00
 *@Version 1.0
 */
@Service
public class ouserServiceImplement implements ouserService {
    @Resource
    ouserMapper ouserMapper;
    @Resource
    doctorMapper doctorMapper;
    @Resource
    volunteerMapper volunteerMapper;
    @Resource
    orderMapper orderMapper;
    @Resource
    checkMapper checkMapper;
    @Resource
    monitorMapper monitorMapper;

    @Override
    public OlderUser loginIn(String phone) {
        QueryWrapper<OlderUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("phone",Long.parseLong(phone));
        return ouserMapper.selectOne(queryWrapper);
    }

    @Override
    public OlderUser queryInfo(Long oid) {
        return ouserMapper.selectById(oid);
    }

    @Override
    public OlderUser updateInfo(OlderUser olderUser) {
        ouserMapper.updateById(olderUser);
        return olderUser;
    }

    @Override
    public List<Doctor> queryDoctors() {
        return doctorMapper.queryDoctors();
    }

    @Override
    public List<Doctor> queryDoctorsBySpecify(String specify) {
        return doctorMapper.queryDoctorBySpecify(specify);
    }

    @Override
    public Doctor queryDoctorById(Long doctorId) {
        return doctorMapper.selectById(doctorId);
    }

    @Override
    public List<Volunteer> queryVolunteers() {
        return volunteerMapper.selectVolunteers();
    }

    @Override
    public Volunteer queryVolunteerById(Long vid) {
        return volunteerMapper.selectById(vid);
    }

    @Override
    public List<Order> queryOrders(Long oid) {
        QueryWrapper<Order> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("accept_id",oid);
        return orderMapper.selectList(queryWrapper);
    }

    @Override
    public List<Check> queryCheck(Long oid) {
        QueryWrapper<Check> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("bindId",oid);
        return checkMapper.selectList(queryWrapper) ;
    }

    @Override
    public monitorHandle queryMonitor(Long oid) {
        QueryWrapper<Monitor> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",oid);
        return new monitorHandle(monitorMapper.selectOne(queryWrapper));
    }

    @Override
    public boolean rateVolunteer(Long oid, Float rating, Long vid) {
        Volunteer volunteer=volunteerMapper.selectById(vid);
        Float current=(rating+volunteer.getRating())/2;
        volunteer.setRating(current);
        return 1==volunteerMapper.updateById(volunteer);
    }

    @Override
    public boolean rateDoctor(Long oid, Float rating, Long did) {
        Doctor doctor=doctorMapper.selectById(did);
        Float current=(doctor.getRating()+rating)/2;
        doctor.setRating(current);
        return 1==doctorMapper.updateById(doctor);
    }

    @Override
    public Monitor queryMonitorById(String oid) {
        QueryWrapper<Monitor> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",Long.parseLong(oid));
        return monitorMapper.selectOne(queryWrapper);
    }

    @Override
    public monitorHandle click(Long oid,Monitor monitor,Long duration) {
        //更新逻辑
        String isUsed=monitor.getIsUsed();
        Integer between= Integer.parseInt(String.valueOf(duration));
        if(isUsed.charAt(between)!='1'){
            StringBuffer stringBuffer=new StringBuffer(isUsed);
            stringBuffer.replace(between,between+1,"1");
            monitor.setIsUsed(stringBuffer.toString());
            monitorMapper.updateById(monitor);
        }
        return new monitorHandle(monitor);
    }

    @Override
    public boolean createVOrder(Long oid, String oName, Long vid, String vName, Float price) {
        Order order=new Order();
        order.setAccptId(oid);
        order.setAcceptName(oName);
        order.setOffId(vid);
        order.setOfferName(vName);
        order.setType("1");
        order.setPrice(price);
        return 1==orderMapper.insert(order);
    }

    @Override
    public boolean createDOrder(Long oid, String oName, Long did, String dName, Float price) {
        Order order=new Order();
        order.setAccptId(oid);
        order.setAcceptName(oName);
        order.setOffId(did);
        order.setOfferName(dName);
        order.setType("0");
        order.setPrice(price);
        return 1==orderMapper.insert(order);
    }

    @Override
    public OlderUser queryInfoByTel(String phone) {
        QueryWrapper<OlderUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("phone",Long.parseLong(phone));
        return ouserMapper.selectOne(queryWrapper);
    }
}
